extends Node
## 专门用来接收一些信号来改变玩家的数据和状态

@export var player: Player
var player_stats: PandoraEntity
var buff_manager: BuffManager

func _ready() -> void:
	EventBus.subscribe("init_player", on_init_player)
	EventBus.subscribe("player_attack_effect", on_player_attack_effect)
	EventBus.subscribe("boss_die", on_boss_die)
	EventBus.subscribe("upgrade", on_upgrade)
	await player.ready
	on_init_player()
	
func _process(_delta: float) -> void:
	if Input.is_action_just_pressed("无敌"):
		Utils.set_player_stats_value("base_damage", float(5.0))
	
func on_player_attack_effect() -> void:
	var add_attack_probability = player_stats.get_float("add_attack_probability")
	var add_coin_probability = player_stats.get_float("add_coin_probability")
	var add_mp_probability = player_stats.get_float("add_mp_probability")
	var add_hp_probability = player_stats.get_float("add_hp_probability")
	var add_exp_probability = player_stats.get_float("add_exp_probability")
	print("攻击效果后")
	## 攻击后按照概率依次去增加
	if randf() < add_attack_probability:
		Utils.set_player_stats_value("base_damage", 0.1)
		Utils.show_fluent_tip("攻击力增加0.1")
		print("攻击力增加0.1")
	if randf() < add_coin_probability:
		Utils.set_player_stats_value("gold", 1)
		Utils.show_fluent_tip("金币增加1")
		print("金币增加1")
	if randf() < add_mp_probability:
		Utils.set_player_stats_value("max_mp", 0.1)
		Utils.show_fluent_tip("魔法值增加0.1")
		show_info("魔法值增加")
		print("魔法值增加0.1")
	if randf() < add_hp_probability:
		Utils.set_player_stats_value("max_hp", 0.1)
		Utils.show_fluent_tip("生命值增加0.1")
		show_info("生命值增加")
		print("生命值增加1")
	if randf() < add_exp_probability:
		var add_xp = Utils.player.player_stats.get_float("exp_bonus") * 1
		Utils.set_player_stats_value("exp", add_xp)
		Utils.show_fluent_tip("经验值增加%s" % add_xp)
		show_info("经验值增加")
		print("经验值增加" + str(add_xp))
	
	## 星速加快攻速50%
	if buff_manager.has_buff("星速"):
		if randf() < 0.1:
			Utils.show_fluent_tip("星速能力触发")
			var buff = BuffFactory.create_attack_speed_buff_percent("星速", 0.5, 50)
			Utils.player.buff_manager.add_buff(buff, Utils.player)
			
	## 幸运金币
	if buff_manager.has_buff("幸运金币"):
		Utils.show_fluent_tip("金币增加1")
		Utils.set_player_stats_value("gold", 1)
	## 屏幕震动
	Utils.camera_shake()
	
## 展示信息
func show_info(text: String):
	Utils.show_string_label_c(text, owner, Color("feeeed"))

func _on_player_stats_changed():
	if Utils.get_player_stats_value("exp") >= Utils.get_player_stats_value("level") * 100:
		EventBus.push_event("upgrade", [])

func on_init_player():
	buff_manager = player.buff_manager
	player_stats = player.player_stats
	player_stats.changed.connect(_on_player_stats_changed)
	player_stats.set_float("current_hp", player_stats.get_float("max_hp"))
	player_stats.set_float("current_mp", player_stats.get_float("max_mp"))
	
## 根据第几个boss	
func on_boss_die():
	Utils.set_player_stats_value("gold", Utils.now_level * 100)
	var add_xp = Utils.now_level * 100 * (Utils.get_player_stats_value("exp_bonus") + 1)
	Utils.set_player_stats_value("exp", add_xp)
	print("击败BOSS后获得经验：" + str(add_xp))
	## 这是关卡层级，不是等级
	Utils.now_level += 1
	## 刷新商店
	EventBus.push_event("refresh_shop")
	RaffleManager.boss_die()
	await get_tree().create_timer(1).timeout		
	## 回到主场景
	EventBus.push_event("to_des", ["main_scene"])
	EventBus.push_event("show_time", [0, false])
	player.timer.queue_free()
	
func on_upgrade():
	## 减少一百经验
	Utils.show_fluent_tip("玩家升级")
	print("[玩家升级！！]")
	Utils.set_player_stats_value("exp", -Utils.get_player_stats_value("level") * 100)
	## 加属性
	var level = Utils.get_player_stats_value("level") as int
	var add_level = 1
	## 额外升一级
	if buff_manager.has_buff("终极之书"):
		level = level * 2 + 1
		add_level += 1
	Utils.set_player_stats_value("max_hp", level * 1)
	Utils.set_player_stats_value("max_mp", level * 1)
	Utils.set_player_stats_value("base_damage", level * 1)
	Utils.set_player_stats_value("attack_speed", level * 2)
	## 升一级
	Utils.set_player_stats_value("level", add_level)

## 获取最近的敌人
func _get_nearest_enemy() -> Node:
	var enemies = get_tree().get_nodes_in_group("enemy")
	var nearest_enemy = null
	var nearest_distance = 1000.0
	
	for enemy in enemies:
		var distance = Utils.player.global_position.distance_to(enemy.global_position)
		if distance < nearest_distance:
			nearest_distance = distance
			nearest_enemy = enemy
	
	return nearest_enemy
